<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
    <script src="./utils/createDraw.js"></script>
    <script src="./utils/LineString.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        #btn {
            width: 200px;
            height: 50px;
            position: fixed;
            z-index: 1000;
            top: 20px;
            left: 50px;
        }
    </style>
</head>

<body>
    <button id="btn" onclick="activeDraw()">激活画笔</button>
    <div id="map">
    </div>
    <script>
        var btn = document.getElementById("btn");
        const docName = "zxd-plus-pla"
        var docLayer = new Zondy.Map.Doc("", docName, {

        })
        var map = new ol.Map({
            target: "map",
            layers: [gaode, docLayer],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 4
            })
        })

        // 1、获取激活画笔的坐标
        let source = new ol.source.Vector({

        })
        let layer = new ol.layer.Vector({
            source
        })
        map.addLayer(layer);
        function activeDraw() {
            /* 设置按钮的样式 */
            btn.disabled = true;
            btn.style.cursor = "no-drop"
            let draw = createDraw({
                type: "LineString",
                source
            })
            map.addInteraction(draw);
            draw.on("drawend", handleDraw)
        }
        function handleDraw(evt) {
            /* 设置按钮的样式 */
            btn.disabled = false;
            btn.style.cursor = "default"
            /* 获取坐标 */
            // 2、调用LineString.add
            let positions = evt.feature.getGeometry().getCoordinates();
            let attr = [];
            let service = {
                name: docName,
                layerId: 1
            }
            LineString.add({
                positions,
                attr,
                service,
                docLayer
            })

            /* 移除画笔 */
            map.getInteractions().pop();
        }

    </script>
</body>

</html>